/*
 * Created on Feb 21, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package fr.jade.service.fm.reactor;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import fr.jade.base.JadeBaseComponent;
import fr.jade.service.fm.deployment.DeploymentTask;
import fr.jade.service.fm.deployment.NewComponentTask;
import fr.jade.service.fm.deployment.SetAttributeTask;
import fr.jade.util.FractalNavigator;
import fr.jade.util.FractalUtil;
import fr.jade.util.JadeException;

public class AttributePolicy extends JadeBaseComponent implements Policy{

  public int getPriorityLevel(){
    return 1;
  }
	
  public Set<DeploymentTask> execute(Map<Component, NewComponentTask> context, Set<Component> failedCmps_M, Set<Component> impactedCmps_M, Component newNode_M) throws JadeException {
    log("Attributes Policy");
	  Set<DeploymentTask> tasks= new HashSet<DeploymentTask>();
	  for (Component failedCmp_M : failedCmps_M) {
	    try{
  		  Map<String, String> attributes = FractalNavigator.getAttributes(failedCmp_M);
  		  log(FractalNavigator.getName(failedCmp_M) + attributes.toString());
  		  NewComponentTask ct = context.get(failedCmp_M);
  		  SetAttributeTask at = new SetAttributeTask(ct, attributes);
  		  tasks.add(at);
	    }catch(NoSuchInterfaceException ignored){
	      log(FractalNavigator.getName(failedCmp_M) + " is not parametric");
	    }catch(NullPointerException e){
	      log(FractalNavigator.getName(failedCmp_M)+" : NPE on listFcAtt()");
	    }
		}
	  return tasks;
	}
	
}
